/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.batchno;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.batchno.BatchNo;
import com.server.modules.sys.service.batchno.BatchNoService;

/**
 * 批次号Controller
 * @author 丁光彪
 * @version 2016-10-31
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/batchno/batchNo")
public class BatchNoController extends BaseController {

	@Autowired
	private BatchNoService batchNoService;
	
	@ModelAttribute
	public BatchNo get(@RequestParam(required=false) String id) {
		BatchNo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = batchNoService.get(id);
		}
		if (entity == null){
			entity = new BatchNo();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:batchno:batchNo:view")
	@RequestMapping(value = {"list", ""})
	public String list(BatchNo batchNo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BatchNo> page = batchNoService.findPage(new Page<BatchNo>(request, response), batchNo); 
		model.addAttribute("page", page);
		model.addAttribute("batchNo", batchNo);
		return "modules/sys/batchno/batchNoList";
	}

	@RequiresPermissions("sys:batchno:batchNo:view")
	@RequestMapping(value = "form")
	public String form(BatchNo batchNo, Model model) {
		model.addAttribute("batchNo", batchNo);
		return "modules/sys/batchno/batchNoForm";
	}

	@RequiresPermissions("sys:batchno:batchNo:edit")
	@RequestMapping(value = "save")
	public String save(BatchNo batchNo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, batchNo)){
			return form(batchNo, model);
		}
		batchNoService.save(batchNo);
		addMessage(redirectAttributes, "保存批次号成功");
		return "redirect:"+Global.getAdminPath()+"/sys/batchno/batchNo/?repage";
	}
	
	@RequiresPermissions("sys:batchno:batchNo:edit")
	@RequestMapping(value = "delete")
	public String delete(BatchNo batchNo, RedirectAttributes redirectAttributes) {
		batchNoService.delete(batchNo);
		addMessage(redirectAttributes, "删除批次号成功");
		return "redirect:"+Global.getAdminPath()+"/sys/batchno/batchNo/?repage";
	}

}